<!DOCTYPE html>
<html lang="cn" xmlns:th="http://www.thymeleaf.org">
<head th:replace="~{baseui/head :: head}">
    <meta charset="utf-8"/>
    <meta name="viewport" content="width=device-width, initial-scale=1.0"/>
    <meta name="description" content="动检通智慧云平台"/>
    <meta name="author" content="牧中(上海)物联网科技有限公司"/>
    <link rel="shortcut icon" href="/webjars/images/favicon.png"/>
    <link rel="stylesheet" href="/webjars/js/layui/css/layui.css"  media="all">

    <title>牧中畜牧业大数据智慧云平台V1.0</title>


</head>
<body>
<!--顶部一级导航-->
<th:block th:replace="baseui/topheader :: topheader(1)"/>

<!--动检通菜单-->
<th:block  th:replace="djt/partial/topnav :: topnav(9)" />


<div id="cl-wrapper">
    <!--左边导航-->
    <th:block th:replace="djt/partial/insurance_left :: nav(4)" />


    <div class="container-fluid" id="pcont">
        <div class="row">
            <div class="col-md-12">
                <div class="block-flat">
                    <div class="header">
                        <h3>发布消息通知</h3>
                    </div>
                    <div class="content">
                        <div class="table-responsive">
                            <div id="datatable_wrapper" class="dataTables_wrapper form-inline dt-bootstrap no-footer">
                                <div class="row">

                                    <span style="margin-left: 40px;">
                                        消息类型:
                                        <select class="select2" style="max-width: 165px;" name="type" id="type">
                                           <option value="1" >投保通知</option>
                                           <option value="2">理赔通知</option>
                                           <option value="3" selected="">系统其他通知</option>
                                        </select>
                                    </span>

                                    <span style="margin-left: 25%">
                                        收消息人:
                                        <select class="select2" multiple style="max-width: 165px;" id="getUserId"></select>
                                    </span>

                                    <div style="margin: 15px 0 10px 40px;">消息内容</div>
                                    <div style="margin-left: 40px">
                                        <textarea style="width: 80%;height: 300px;" placeholder="请输入通知内容" name="message"></textarea>
                                    </div>

                                    <input type="hidden" name="set_message_name" th:value="${username}"/>
                                    <input type="hidden" name="set_message_id"  id="set_message_id" th:value="${userId}"/>

                                    <div style="margin-left: 40px">
                                        <button class="btn btn-primary" id="pushBtn" style="margin-top: 10px;margin-left: 40%;">发布</button>
                                    </div>
                                    <span id="mes" style="margin-left: 248px;color: red;font-size: 18px;"></span>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<script type="text/javascript" src="/webjars/js/jquery.js"></script>

<script type="text/javascript" src="/webjars/js/jquery.nanoscroller/jquery.nanoscroller.js"></script>
<script type="text/javascript" src="/webjars/js/behaviour/general.js"></script>
<script src="/webjars/js/jquery.ui/jquery-ui.js" type="text/javascript"></script>
<script type="text/javascript" src="/webjars/js/jquery.nestable/jquery.nestable.js"></script>

<script type="text/javascript" src="/webjars/js/bootstrap.switch/bootstrap-switch.min.js"></script>
<script type="text/javascript" src="/webjars/js/bootstrap.datetimepicker/js/bootstrap-datetimepicker.min.js"></script>
<script src="/webjars/js/jquery.select2/select2.min.js" type="text/javascript"></script>

<script src="/webjars/js/bootstrap.slider/js/bootstrap-slider.js" type="text/javascript"></script>
<script type="text/javascript" src="/webjars/js/jquery.niftymodals/js/jquery.modalEffects.js"></script>
<script type="text/javascript" src="/webjars/js/bootstrap.summernote/dist/summernote.min.js"></script>




<script type="text/javascript" src="/webjars/js/jquery.magnific-popup/dist/jquery.magnific-popup.min.js"></script>



<script src="/webjars/js/behaviour/voice-commands.js"></script>
<script src="/webjars/js/bootstrap/dist/js/bootstrap.min.js"></script>
<script type="text/ecmascript" src="/webjars/jqgrid/js/i18n/grid.locale-cn.js"></script>
<script type="text/ecmascript" src="/webjars/jqgrid/js/jquery.jqGrid.min.js"></script>
<script src="/webjars/js/layui/layui.js" charset="utf-8"></script>

<script type="text/javascript">


    $(document).ready(function() {

        App.init();
        if ($("#set_message_id").val() != "") {
            allUser();
        }
    })

    //根据当前用户id获取其公司的所有员工
    function allUser(){
        $.ajax({
            url:"/djt/insurance/getUserList/ajax",
            data:{userId:$("#set_message_id").val()},
            dataType:"json",
            success:function (data) {
                $("#getUserId").empty();
                if (data.length != 0) {
                    var allUser = " <option value='-1'>所有人员</option>";
                    $("#getUserId").append(allUser);
//                    for (var o in data){
//                        var id = data[o].userId;
//                        var name = data[o].userName;
//                        var str = "<option value=" + id + ">" + name + "</option>";
//                        $("#getUserId").append(str);
//                    }
//                    var arrs = eval(data);
//                    for (var i=0;i<data.length;i++){
//                        var id = data[i].userId;
//                       var name = data[i].userName;
//                        var str = "<option value=" + id + ">" + name + "</option>";
//                        $("#getUserId").append(str);
//                    }


//                    $.each(data, function (index, obj) {
//                        var id = obj.userId;
//                        var name = obj.userName;
//                        var str = "<option value=" + id + ">" + name + "</option>";
//                        $("#getUserId").append(str);
//                    });
                }
            }
        })
    }

    //在发布时获取需要接收通知消息的人员集合
    function getUserOption() {
        var userIdList = $("#getUserId").val();
        if (userIdList == -1) {
            var array1 = new Array();
            $("#getUserId option").each(function () {  //遍历所有option
                var txt = $(this).val();   //获取option值
                if (txt != '' && txt != -1) {
                    array1.push(txt);  //添加到数组中
                }
            });
            return array1.toString();
        }else if (userIdList == 0){
            var array3 = new Array();
            array3.push(userIdList);
            return array3.toString();
        }else {
            var array2 = new Array();
            array2.push(userIdList);
            return array2.toString();
        }
    }

    // 发布
    $("#pushBtn").click(function(){

        layui.use(['laypage', 'layer'], function () {
            var $ = layui.jquery, layer = layui.layer;
            if ($("input[name='set_message_name']").val() == ""
                || $("input[name='set_message_id']").val() == ""
                || $("#getUserId").val() == ""
                || $("textarea[name='message']").val() == ""){
                layer.msg('发布失败,请重试', {
                    time: 60000, //20s后自动关闭
                    btn: '确定',
                    btnAlign: 'c'
                })
            }else {
                layer.msg('确定发布吗？', {
                    time: 60000, //20s后自动关闭
                    btn: ['确定', '取消'],
                    btnAlign: 'c',
                    btn1: function (index, layero) {
                        $.ajax({
                            url: "/djt/insurance/addInform/ajax",
                            data: {
                                "type": $("#type").val(),
                                "message": $("textarea[name='message']").val(),
                                "set_message_name": $("input[name='set_message_name']").val(),
                                "set_message_id": $("input[name='set_message_id']").val(),
                                "userIdList": getUserOption()
                            },
                            dataType: "json",
                            success: function (data) {
                                if (data.status == 0){
                                    layer.msg('发布失败请重试', {
                                        time: 60000, //20s后自动关闭
                                        btn: '确定',
                                        btnAlign: 'c',
                                        btn1: function (index, layero) {
                                            window.location.href ="/djt/insurance/inform";
                                        }
                                    })
                                }
                                else{
                                    layer.msg('发布成功', {
                                        time: 60000, //20s后自动关闭
                                        btn: '确定',
                                        btnAlign: 'c',
                                        btn1: function (index, layero) {
                                            window.location.href ="/djt/insurance/inform";
                                        }
                                    })
                                }
                            }
                        })
                    }
                })
            }
        })
    })

</script>
</body>
</html>